// 存储用户数据的简单对象（实际应用中应使用数据库）
let users = {};

// 注册功能
document.getElementById('registerForm').addEventListener('submit', function(event) {
    event.preventDefault();
    const username = document.getElementById('newUsername').value;
    const password = document.getElementById('newPassword').value;
    if (!users[username]) {
        users[username] = password;
        alert('注册成功！');
        window.location.href = 'login.html';
    } else {
        alert('该用户名已存在！');
    }
});

// 登录功能
document.getElementById('loginForm').addEventListener('submit', function(event) {
    event.preventDefault();
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    if (users[username] === password) {
        localStorage.setItem('user', username);
        window.location.href = 'index.html';
    } else {
        alert('用户名或密码错误！');
    }
});

// 显示用户名
if (document.readyState === 'complete' || (document.readyState !== 'loading' && !document.documentElement.doScroll)) {
    showUsername();
} else {
    document.addEventListener('DOMContentLoaded', showUsername);
}

function showUsername() {
    const username = localStorage.getItem('user');
    if (username) {
        document.getElementById('usernameDisplay').textContent = username;
    } else {
        window.location.href = 'login.html';
    }
}